
clear all
set more off
pause on
set trace off
cap log close
set matsize 5000

loc dir "/Users/mateo/Desktop/"
	
	cd "/Users/mateo/Desktop/"
	

//Install Programs

foreach var in  margeff estout avg_effect sutex parmest binscatter ranktest ivreg2 cmp {

	cap which `var'      // check whether -ado- is installed

	qui if _rc ssc install `var', replace // and get it if not
}


log using `"`dir'\Logs\Tables_`c(current_date)'"', append text		//Log File in text format


	  
	

***************************** Violence and Land Titles (Main Results) *****************************

//1. Descriptive Statistics Table

	// * Dependent Variables *//

		use "/Users/mateo/Desktop/master_violence_landtitle_caracteristicas.dta", clear

	/***********VARIABLE LABELS************/
	
	
	* Outcome variables
		
		egen total_violence=rowtotal( sumattacks_public sumattacks_guerrilla sumattacks_paramilitary)
		replace total_violence=. if ( sumattacks_public==. & sumattacks_guerrilla==. & sumattacks_paramilitary==.)
		
		egen health_inst=rowtotal(health_post health_center hospital)

	* Renaming Variables 
		ren area_resguardos_indigenas_cs indian_title_area_cs
		ren area_resguardos_indigenas indian_title_area
		ren area_titul_comunidades_negras_cs afro_title_area_cs
		ren area_titul_comunidades_negras afro_title_area
	
	* Locals

		loc violence_outcomes  total_violence sumattacks_public sumattacks_paramilitary sumattacks_guerrilla 
		loc treatment_vars any_title total_land_title landtitle  landtitle_tally
		
	* Variable Labels
		lab var total_violence "All attacks"
		lab var sumattacks_public "Police \& Army attacks"
		lab var sumattacks_paramilitary "Paramilitary attacks"
		lab var sumattacks_guerrilla "Guerrilla attacks"
		lab var any_title "Any Title (0/1)"
	
	/************ BALANCE VARS ***********/

	// * Violence DVs *//
	
	preserve
	
		loc descriptive_test_vars `violence_outcomes'
		
		estimates clear
		
				
			gen T=_n 
				
				foreach var of loc descriptive_test_vars {
				
					qui eststo E`var'B: xi: reg `var' T , r 
					mat mu_C = _b[_cons]
					mat mu_T = _b[_cons] + _b[T]
					mat clust = e(N_clust)
					
					qui summ `var',d
					mat mean = r(mean)
					mat sd = r(sd)
					mat min = r(min)
					mat max = r(max)
					mat obs = r(N)

					foreach stat in mean sd min max obs {
						mat coln `stat' = T
						qui estadd mat `stat' = `stat'
					}
					
				
				}
				
				
				// Results Table

				local var "total_violence"
				label var T `"\textit{Panel A: Dependent Variables}  \\  `: variable label `var'' "'	
				
 				esttab E`var'B using "/Users/mateo/Desktop/TableA1.tex", cells("mean(fmt(3)) sd(fmt(2)) min(fmt(2)) max(fmt(2)) obs(fmt(%9.0gc))") ///
 				keep(T)  booktabs starlevels(* 0.10 ** 0.05 *** 0.01) eqlabels(none) f label nodep nonum noobs nonotes /*nolines*/ nomtitles  nogaps replace ///
 				collabels("\multicolumn{1}{c}{\specialcell{[1] \\ Mean}}" "\multicolumn{1}{c}{\specialcell{[2] \\ Std. Dev.}}" ///
 				"\multicolumn{1}{c}{\specialcell{[3] \\ Min}}" "\multicolumn{1}{c}{\specialcell{[4] \\ Max}}" "\multicolumn{1}{c}{\specialcell{[5] \\ Obs.}}")  			
				local descriptive_vars_latex `balance_test_vars'
				local descriptive_vars_latex: subinstr local descriptive_test_vars "`var'" ""
				
					foreach var of loc descriptive_vars_latex {
					
							label var T `"`: variable label `var''"'
							esttab E`var'B using "/Users/mateo/Desktop/TableA1.tex", cells("mean(fmt(3))  sd(fmt(2)) min(fmt(2)) max(fmt(2)) obs(fmt(%9.0gc))")	 ///
								keep(T)  booktabs starlevels(* 0.10 ** 0.05 *** 0.01) eqlabels(none) label f nodep nonum noobs nonotes nolines nomtitles  nogaps append ///
								collabels(, none)				
					}
					
	restore 
				
	// * Treatment Vars *//
		
	preserve
		
		lab var any_title "\\ \textit{Panel B: Land Titling Variables} \\  Any Title (0/1)"
			
		loc descriptive_test_vars  `treatment_vars'       
				
			gen T=_n 
				
				foreach var of loc descriptive_test_vars {
				
					qui eststo E`var'B: xi: reg `var' T , r 
					mat mu_C = _b[_cons]
					mat mu_T = _b[_cons] + _b[T]
					mat clust = e(N_clust)
					
					qui summ `var',d
					mat mean = r(mean)
					mat sd = r(sd)
					mat min = r(min)
					mat max = r(max)
					mat obs = r(N)

					foreach stat in mean sd min max obs {
						mat coln `stat' = T
						qui estadd mat `stat' = `stat'
					}
				
				}
				
				
				// Results Table

				foreach var of loc descriptive_test_vars {
					
							label var T `"`: variable label `var''"'
							esttab E`var'B using "/Users/mateo/Desktop/TableA1.tex", cells("mean(fmt(3))  sd(fmt(2)) min(fmt(2)) max(fmt(2)) obs(fmt(%9.0gc))")	 ///
								keep(T)  booktabs starlevels(* 0.10 ** 0.05 *** 0.01) eqlabels(none) label f nodep nonum noobs nonotes nolines nomtitles  nogaps append ///
								collabels(, none)				
					}
	
	restore
